﻿using HZ.Public;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Data;

namespace WpfAppDemo
{
    public class RuleType2Bool : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return value == null ? false : value.ToString().Equals(parameter);
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
        return value != null && value.Equals(true) ? PublicClass.ToInt( parameter) : Binding.DoNothing;
        }
    }

    public class RuleData
    {
        public static ObservableCollection<RuleProtoclClass> RuleList = null;
    }

    public class RuleProtoclClass : ObservableBase
    {

        private int _Type = 0;

        public int Type //策略类型 0 - 按时间 1 - 按数量
        {
            get { return _Type; }
            set { _Type = value; RaisePropertyChanged("Type"); }
        }

        private int _ActType = 0;

        public int ActType  //执行动作 0 - 删除 1 - 备份
        {
            get { return _ActType; }
            set { _ActType = value; RaisePropertyChanged("ActType"); }
        }

        private int _MaxMonth = 3;

        public int MaxMonth //时间上限
        {
            get { return _MaxMonth; }
            set { _MaxMonth = value; RaisePropertyChanged("MaxMonth"); }
        }

        private int _MaxCount = 10;

        public int MaxCount //数量上限
        {
            get { return _MaxCount; }
            set { _MaxCount = value; RaisePropertyChanged("MaxCount"); }
        }

        private int _StepType = 0;

        public int StepType
        {
            get { return _StepType ; }
            set { _StepType = value; RaisePropertyChanged("StepType"); }
        }


        private int _Step = 1;

        public int Step
        {
            get { return _Step; }
            set { _Step = value; RaisePropertyChanged("Step"); }
        }

        private int _Time = 0;

        public int Time
        {
            get { return _Time; }
            set { _Time = value; RaisePropertyChanged("Time"); }
        }

        private string _RuleDesc;

        public string RuleDesc //规则描述
        {
            get { return _RuleDesc; }
            set { _RuleDesc = value; RaisePropertyChanged("RuleDesc"); }
        }



        //private bool _IsSelected = false;
        //[Newtonsoft.Json.JsonIgnore]
        //public bool IsSelected   //是否被选择
        //{
        //    get { return _IsSelected; }
        //    set { _IsSelected = value; RaisePropertyChanged("IsSelected"); }
        //}

        private bool _IsRun = false;

        public bool IsRun    //是否启用
        {
            get { return _IsRun; }
            set { _IsRun = value; RaisePropertyChanged("IsRun"); }
        }

        private string _LastExecTime = "---";

        public string LastExecTime   //最后执行时间
        {
            get { return _LastExecTime; }
            set { _LastExecTime = value; RaisePropertyChanged("LastExecTime"); }
        }

        private string _NextExecTime = "---";

        public string NextExecTime  //下一次执行时间
        {
            get { return _NextExecTime; }
            set { _NextExecTime = value; RaisePropertyChanged("NextExecTime"); }
        }

        public string CurrFileName { get; set; }

        private bool DeleteDB(string exec_date)
        {
            bool ret = false;
            string strSQL = "calibrationno < \"" + exec_date + "\"";
            ret = DBClass.Delete(DBClass.strCalibrationIndexTableName, strSQL);
            strSQL = "calibrationno < \"" + exec_date + "\"";
            ret = DBClass.Delete(DBClass.strCalibrationTableName, strSQL);
            strSQL = "calibrationno < \"" + exec_date + "\"";
            ret = DBClass.Delete(DBClass.strCaliFuYaIndexTableName, strSQL);
            strSQL = "StartTestTime < \"" + exec_date + "\"";
            ret = DBClass.Delete(DBClass.strDCIRTableName, strSQL);
            strSQL = "SoeTime < \"" + exec_date + "\"";
            ret = DBClass.Delete(DBClass.strNGTableName, strSQL);
            strSQL = "StartTestTime < \"" + exec_date + "\"";
            ret = DBClass.Delete(DBClass.strTestDataTableName, strSQL);
            strSQL = "Time < \"" + exec_date + "\"";
            ret = DBClass.Delete(DBClass.strWireOrderTableName, strSQL);
            return ret;
        }

        private List<FileSystemInfo> GetSubDir(string path, string pattern = "*")
        {
            List<FileSystemInfo> ret = new List<FileSystemInfo>();
            //绑定到指定的文件夹目录
            DirectoryInfo dir = new DirectoryInfo(path);

            //检索表示当前目录的文件和子目录
            FileSystemInfo[] fsinfos = dir.GetFileSystemInfos();

            foreach (FileSystemInfo item in fsinfos)
            {
                if (item is DirectoryInfo)
                {
                    ret.Add(item);
                }
            }
            return ret;
        }

        private bool DeleteFile(string exec_date)
        {
            
            bool ret = false;
            string path = PublicClass.projectConfig.system.strHisDataDir;
           
            string curr_day = Convert.ToDateTime(exec_date).ToString("yyyyMMdd");
            List<FileSystemInfo>  root  = GetSubDir(path);
            foreach (var item in root) //年月
            {
                List<FileSystemInfo> daylist = GetSubDir(item.FullName);
                foreach(var day in daylist) //日
                if (Convert.ToInt32(item.Name+day.Name) < Convert.ToInt32( curr_day)) //符合条件

                {
                        PublicClass.DeleteDir(day.FullName);
                }
            }
            return ret;
        }

        private bool BackupDB()
        {
            CurrFileName = DateTime.Now.ToString("yyyyMMddHHmmss");
            string path = PublicClass.BackupDir + CurrFileName + "\\";
            List<string> ziplist = new List<string>();
            if (Directory.Exists(path) == false)
            {
                Directory.CreateDirectory(path);
            }
            DBClass.SaveCopy(DBClass.strCalibrationIndexTableName, path+ DBClass.strCalibrationIndexTableName+".sql");
            DBClass.SaveCopy(DBClass.strCalibrationTableName, path+ DBClass.strCalibrationTableName + ".sql");
            DBClass.SaveCopy(DBClass.strCaliFuYaIndexTableName, path+ DBClass.strCaliFuYaIndexTableName + ".sql");
            DBClass.SaveCopy(DBClass.strDCIRTableName, path+ DBClass.strDCIRTableName + ".sql");
            DBClass.SaveCopy(DBClass.strNGTableName, path+ DBClass.strNGTableName + ".sql");
            DBClass.SaveCopy(DBClass.strTestDataTableName, path+ DBClass.strTestDataTableName + ".sql");
            DBClass.SaveCopy(DBClass.strWireOrderTableName, path+ DBClass.strWireOrderTableName + ".sql");
            PublicClass.ZipFileDirectory(path, PublicClass.BackupDir + CurrFileName + ".dat",null);
            PublicClass.DeleteDir(path);
            return true;
        }

        private bool BackupFile()
        {
            string path = PublicClass.projectConfig.system.strHisDataDir;
            if (Directory.Exists(path))
            {
                PublicClass.ZipFileDirectory(path, PublicClass.BackupDir + CurrFileName + ".fls", null);
            }
            
            return false;
        }

        public bool RunAutoDelete(string exec_date)
        {
            bool ret = false;
            if (DeleteDB(exec_date))
            {
                ret = DeleteFile(exec_date);
            }
            return ret;
        }

        public bool RunAutoBackup()
        {
            bool ret = false;
            if (BackupDB())
            {
                ret = BackupFile();
            }
            return ret;
        }

        public string GetNextExecTime(string exec, RuleProtoclClass obj)
        {
            string ret = "";
            DateTime cur = DateTime.Now;
            if (exec != "---")
            {
                cur = Convert.ToDateTime(exec);
            }

            DateTime next = DateTime.Now;
            switch (obj.StepType)
            {
                case 0:
                    next = cur.AddDays(obj.Step);
                    break;
                case 1:
                    next = cur.AddDays(obj.Step * 7);
                    break;
                case 2:
                    next = cur.AddMonths(obj.Step);
                    break;
                case 3:
                    next = cur.AddYears(obj.Step);
                    break;
                default:
                    break;
            }
            ret = next.ToString("yyyy-MM-dd ") + obj.Time.ToString().PadLeft(2, '0') + ":00:00.000";
            return ret;
        }
    }
}
